/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[][]}
 */
var fourSum = function (nums, target) {
    let n = nums.length;
    if (n < 4) {
        return []
    }
    if (n === 4) {
        return sum(nums) === target ? [nums] : []
    }

    nums = nums.sort((a, b) => { a - b });

    let res = [];
    for (let i = 0; i <= (n - 4); i++) {
        let arr = [];
        arr.push(nums[i]);
        console.log('initial', arr, i)
        recur(arr, res, i + 1, 1, target, nums);
    }

    console.log(res);
    return res;
};

var sum = function (arr) {
    let res = 0;
    for (let i = 0; i < 4; i++) {
        res += arr[i];
    }
    return res;
}

var arrCompare = function (res, arr) {
    for (let i = 0; i < res.length; i++) {
        if (String(res[i].sort((a, b) => (a - b))) === String(arr.sort((a, b) => (a - b)))) {
            return true
        }
    }
    return false
}

var recur = function (arr, res, index, len, target, nums) {
    if (len === 4) {
        if (sum(arr) === target && !arrCompare(res, arr)) {
            console.log('arr', arr);
            res.push(arr);
        }
        return
    }
    for (let i = index; i < nums.length; i++) {
        let temp = [].concat(arr);
        temp.push(nums[i]);
        recur(temp, res, i + 1, temp.length, target, nums);
    }
}

fourSum([2, 2, 2, 2, 2], 8);